package random.fox.p2p.ui;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;
import random.fox.p2p.App;
import random.fox.p2p.R;
import random.fox.p2p.event.ActionBarEvent;
import random.fox.p2p.event.ProgressEvent;
import random.fox.p2p.model.UserInfoModule;
import random.fox.p2p.pathview.HandlesBack;
import random.fox.p2p.pathview.Utils;
import random.fox.p2p.presenter.UserInfoPresenter;
import random.fox.p2p.util.Constants;
import random.fox.p2p.view.IUserInfoView;

/**
 * Created by acer on 2015/3/22.
 */
public class UserInfoView extends LinearLayout implements IUserInfoView,HandlesBack {
    private final String TAG = "UserInfoView";

    @Inject
    UserInfoPresenter userInfoPresenter;

    @InjectView(R.id.grid_view)
    GridView mGridView;
    @InjectView(R.id.user_info_toolbar)
    Toolbar toolbar;
    @InjectView(R.id.user_info_age )
    TextView ageText ;
    @InjectView(R.id.user_info_city )
    TextView cityText ;
    @InjectView(R.id.user_info_school )
    TextView schoolText ;



    private int gridViewWidth , gridViewHeight;

    public UserInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.inject(this);
        ((App) getContext().getApplicationContext()).createScopedGraph(new UserInfoModule(this)).inject(this);

        toolbar.setTitle("用户*信息");
        EventBus.getDefault().post(new ActionBarEvent(toolbar, R.menu.menu_user_info , true));

        System.out.println("TT onFinishInflate:"+System.currentTimeMillis());
        init();
    }

    private void init(){
        this.measure(0, 0);
        int measuredWidth = this.getMeasuredWidth();

        gridViewWidth = measuredWidth * 9 / 4;
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mGridView.getLayoutParams();
        linearParams.width = gridViewWidth + 120;
        mGridView.setLayoutParams(linearParams);
        mGridView.setNumColumns(3);
        mGridView.setColumnWidth(gridViewWidth/3);
        mGridView.setStretchMode(GridView.NO_STRETCH);

        Utils.waitForMeasure(mGridView, new Utils.OnMeasuredCallback() {
            @Override
            public void onMeasured(View view, int width, int height) {
                System.out.println("TT onMeasured:" + System.currentTimeMillis());
                int padding = mGridView.getPaddingBottom();
                gridViewHeight = height - padding * 2;
                userInfoPresenter.loadData(mGridView, gridViewWidth, gridViewHeight, Constants.TYPE.MIXED);

            }
        });
    }

    @Override
    public void showProgress() {
        EventBus.getDefault().post(new ProgressEvent(true));
    }

    @Override
    public void hideProgress() {
        EventBus.getDefault().post(new ProgressEvent(false));
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    public boolean onBackPressed() {
        userInfoPresenter.releaseData(); //退出当前界面了
        return false;
    }

    @Override
    public void fillData(String age, String city, String school) {
        ageText.setText(age);
        cityText.setText(city);
        schoolText.setText(school);
    }
}
